package com.example.springalibabacloudclient.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.ConstantString;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * @Classname FeignClientInterceptor
 * @Description TODO
 * @Date 2022/3/22 23:45
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Slf4j
@Component
public class FeignClientInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication= securityContext.getAuthentication();
        System.out.println(securityContext.getAuthentication().getPrincipal());
        if(authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails){
            OAuth2AuthenticationDetails auth=(OAuth2AuthenticationDetails)authentication.getDetails();
            String value=auth.getTokenValue();
           requestTemplate.header("Authorization",String.format("%s %s","Bearer",value));
            //requestTemplate.header("Authorization","Bearer 7135da97-fb95-43bd-8c7c-26dae356221c");
        }

    }

}
